////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                                //
//                    Linked Stack's push, pop, create, destory functions' implemention code               //
//                                                                                                                                //
//                                                                                                                                //
//                        Author:    Jack Hui (huiguobin)                                                               //
//                                                                                                                                //
//                                                                                                                                //
////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////functions' implemention file////////////////////////////////

//warning!!!
/*
I did not alloc the space for the "data" in each node of the linked stack, you must allocate space for
each node. Also make sure that the address of "data" in each node is different!
*/


#include "single_linked_stack.h"

int push(LinkedNode **stack, void *data)
{
	LinkedNode *temp = NULL;
	temp = (LinkedNode *)malloc(sizeof(LinkedNode));
	if(NULL == temp)
	{
		return -1;
	}

	temp->data = data;
	temp->link = *stack;
	*stack = temp;
	return 0;
}

int pop(LinkedNode **stack, void **data)
{
	LinkedNode *temp = NULL;
	if(!(temp = *stack))
	{
		return -1;
	}

	*data = temp->data;
	*stack = temp->link;
	free(temp);
	return 0;
	
}
int create(LinkedNode **stack)
{
	*stack = NULL;
	return 0;
}
int destory(LinkedNode *stack)
{
	LinkedNode *temp = stack;
	LinkedNode *temp1 = NULL;
	while(NULL != temp)
	{
		temp1 = temp;
		temp = temp->link;
		free(temp1);
		temp1 = NULL;
	}
	return 0;
}

int is_stack_empty(LinkedNode *stack)
{
	if(NULL == stack)
	{
		return 1;
	}
	return 0;
}